import pygame

SCREEN_SIZE = (480, 700)
TITLE = '飞机大战'

class GameSprite(pygame.sprite.Sprite):
    def __init__(self, filename, speed=1):
        super().__init__()
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self, *args):
        self.rect.y += self.speed

class BgSprite(GameSprite):
    def __init__(self, is_alt = False):
        super().__init__('./image/bg.png', 1)
        if is_alt:
            self.rect.y = -self.rect.height

    def update(self, *args):
        super().update()
        if self.rect.y > self.rect.height:
            self.rect.y = -self.rect.height


class PlaneGame:

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption(TITLE)
        self.is_loop = True
        self.clock = pygame.time.Clock()
        self.__create_sprites()

    def start(self):
        """开始游戏"""
        # y=0
        while self.is_loop:
            self.__event_handler()
            self.__update_sprites()
            pygame.draw.circle(self.screen, (255,0,0),(240,600), 20)
            self.clock.tick(40)
            pygame.display.update()
        pygame.quit()

    def __event_handler(self):
        """事件处理"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False

    def __create_sprites(self):
        """创建精灵"""
        bg1 = BgSprite()
        bg2 = BgSprite(True)
        self.bg_group = pygame.sprite.Group(bg1, bg2)

    def __update_sprites(self):
        """更新精灵"""
        self.bg_group.update()
        self.bg_group.draw(self.screen)


if __name__ == '__main__':
    plane = PlaneGame()
    plane.start()